package com.shj.em.biz.sell.controller;

import com.shj.em.biz.company.entity.Company;
import org.springframework.stereotype.Controller;

import java.util.List;
import java.util.Map;
import java.util.Arrays;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.shj.em.common.annotation.Log;
import com.shj.em.base.support.ResMsg;
import com.shj.em.base.vo.PageVO;

import com.shj.em.biz.sell.entity.Waifa;
import com.shj.em.biz.sell.service.WaifaService;
import com.baomidou.mybatisplus.plugins.Page;
import lombok.extern.slf4j.Slf4j;
import com.shj.em.base.controller.BaseController;


/**
 * <p>
 * 外发信息 前端控制器
 * </p>
 *
 * @author Shen Huang Jian
 * @since 2018-07-14
 */
@Controller
@Slf4j
@RequestMapping("/waifa")
public class WaifaController extends BaseController<Waifa>{

    private String prefix = "order/waifa";

    @Autowired
    WaifaService waifaService;

    @GetMapping("/gotoSearchOrder")
    public String gotoSearch(){
        return "redirect:/order/gotoSearchWO";
    }

    @GetMapping("/gotoSearch/{orderId}")
    public String gotoSearch(@PathVariable("orderId") Long orderId, Model model){
        addViewOrderInfo(orderId, model);
        addRoleInfo(model);
        return "order/order/add_additional";
    }

    @RequestMapping("/list")
    @ResponseBody
    public PageVO<Waifa> list(@RequestBody Map<String, Object> params){
        Page<Waifa> page = waifaService.queryWaifaPage(params);
        PageVO<Waifa> pageVO = new PageVO<>();
        pageVO.setTotal(page.getTotal());
        pageVO.setRows(page.getRecords());
        return pageVO;
    }

    @GetMapping("/gotoAdd/{orderId}")
	String gotoAdd(@PathVariable Long orderId, Model model){
        Waifa waifa = new Waifa();
        waifa.setOrderId(orderId);
        model.addAttribute("isEdit", false);
        model.addAttribute("waifa", waifa);

        List<Company> supplies = this.companyService.getAllSupplies();
        model.addAttribute("supplies", supplies);
	    return prefix + "/add";
	}

	@GetMapping("/gotoEdit/{id}")
	String gotoEdit(@PathVariable("id") Long id, Model model){
        Waifa waifa = waifaService.selectById(id);
		model.addAttribute("waifa", waifa);
        model.addAttribute("isEdit", true);

        List<Company> supplies = this.companyService.getAllSupplies();
        model.addAttribute("supplies", supplies);
	    return prefix + "/add";
	}

	@Log("创建外发信息")
	@ResponseBody
	@PostMapping("/add")
	public ResMsg add(Waifa waifa){
        try{
            waifaService.insert(waifa);
            return ResMsg.ok(new Waifa());
        }catch(Exception e){
            log.error("创建外发信息失败", e);
            return ResMsg.error("创建外发信息失败");
        }

	}

    @Log("更新外发信息")
	@ResponseBody
	@PostMapping("/edit")
	public ResMsg edit(Waifa waifa){
        try{
            waifaService.updateById(waifa);
            return ResMsg.ok();
        }catch(Exception e){
            log.error("更新外发信息失败", e);
            return ResMsg.error("更新外发信息失败");
        }

	}

    @Log("删除外发")
	@PostMapping( "/remove")
	@ResponseBody
	public ResMsg remove(@RequestParam("id") Long id){
        try{
            waifaService.deleteById(id);
            return ResMsg.ok();
        }catch(Exception e){
            return ResMsg.error();
        }
	}

    @Log("批量删除外发")
	@PostMapping( "/batchRemove")
	@ResponseBody
	public ResMsg batchRemove(@RequestParam("ids[]") Long[] ids){
        waifaService.deleteBatchIds(Arrays.asList(ids));
		return ResMsg.ok();
	}

}

